package com.accenture.portal.controller.access;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JsonConfig;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.accenture.portal.model.access.Access;
import com.accenture.portal.service.access.AccessService;
import com.accenture.portal.util.DateJsonValueProcessor;
import com.accenture.portal.util.IntegerEditor;

/**
 * @author ling.hao
 *
 */
@Controller
public class AccessController {

	@Autowired
	private AccessService accessService;

	/**
	 * search JsonAccess
	 * 
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @throws Exception
	 */

	@RequestMapping(value = "searchJsonAccess.action")
	// @ResponseBody
	public String searchJsonAccess(@RequestParam("number") int number, HttpServletRequest request,
			HttpServletResponse response, Model model) throws Exception {
		
		int page = 5;//每页显示条数
		List<Access> myAccess = accessService.SearchAllAccessStrPagenation((number - 1)
				* page, page);
		//List<Access> myAccess = accessService.SearchAllAccess();
		
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.registerJsonValueProcessor(Date.class,
				new DateJsonValueProcessor("MM-dd-yyyy HH:mm:ss"));

		model.addAttribute("AccessList", myAccess);
		response.getWriter().write(new JSONArray().fromObject(myAccess, jsonConfig).toString());
		response.getWriter().flush();//清空缓冲区，完成写入 return null;
		return null;
	}

	/**
	 * search Access list
	 * 
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "searchAccessList.action")
	public String searchAllAccess(@RequestParam("number") int number, HttpServletRequest request,
			HttpServletResponse response, Model model) throws Exception {
		
		int page = 5;//每页显示条数
		int total = accessService.getTotalCount() ;//总条数
		int count = total / page; //总页数
		
		if (total % page != 0) {  
			count++;              //总页数
		}
		
		if(count==0){ 
			count=1;//数据库没有数据时显示在总共1页
		}
		
		if (number < 1) {//当前最小页数限制
			number = 1;
		} else if (number > count) {
			number = count;//当前最大页数限制
		}
		
		request.getSession().setAttribute("number", number);
		request.getSession().setAttribute("count", count);
		
		return "access/accessBak";
	}

	/**
	 * Add access
	 * 
	 * @param request
	 * @param response
	 * @param model
	 * @param access
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "addAccess.action")
	public String addAccess(
			@RequestParam("accessCreateTime") String accessCreateTimeString,
			@RequestParam("accessCreater") String accessCreater,
			@RequestParam("accessName") String accessName,
			@RequestParam("accessParentId") int accessParentId,
			@RequestParam("modelId") int modelId,
			@RequestParam("acesssDeleteFlag") Boolean acesssDeleteFlag,
			
			HttpServletRequest request, HttpServletResponse response,
			Model model) throws Exception {

		Access access = new Access();

		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date accessCreateTime = sdf.parse(accessCreateTimeString);
			access.setAccessCreateTime(accessCreateTime);
		} catch (ParseException e) {
			System.out.println(e.getMessage());
		}

		access.setAccessCreater(accessCreater);
		access.setAccessName(accessName);
		access.setAcesssDeleteFlag(acesssDeleteFlag);
		access.setModelId(modelId);
		access.setAccessParentId(accessParentId);
		
		accessService.AddAccess(access);
		int number = (int) request.getSession().getAttribute("count");
		return "redirect:searchAccessList.action?number=" + number;
	}

	
	/**
	 * Go to AddAccess
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "goToAddAccess.action")
	public String GotoAddAccess(HttpServletRequest request,
			HttpServletResponse response) {
		return "access/addAccess";
	}

	/**
	 * update access
	 * 
	 * @param request
	 * @param response
	 * @param access
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "updateAccess.action")
	public String updateAccess(HttpServletRequest request,
			HttpServletResponse response, Access access) throws Exception {

		accessService.UpdateAccess(access);
		int number = (int) request.getSession().getAttribute("number");
		return "redirect:searchAccessList.action?number=" + number;
	}

	
	/**
	 * go To UpdateAccess
	 * 
	 * @param accessId
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @throws ParseException
	 */
	@RequestMapping(value = "goToUpdateAccess.action")
	public String goToUpdateAccess( Integer accessId,
			HttpServletRequest request, HttpServletResponse response,
			Model model) throws ParseException {
		
		Access editAccess = accessService.SearchAccessById(accessId);
		Date accessCreateTime = editAccess.getAccessCreateTime();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String accessCreateTimeLast = sdf.format(accessCreateTime);
		
		request.getSession().setAttribute("accessCreateTimeLast",
				accessCreateTimeLast);
		model.addAttribute("editAccess", editAccess);
		return "access/UpdateAccess";
	}

	/**
	 * physical Delete Access
	 * 
	 * @param request
	 * @param response
	 * @param accessId
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "physicalDeleteAccess.action")
	public String physicalDeleteAccess(
			@RequestParam("accessId") Integer accessId,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		accessService.physicalDeleteAccess(accessId);
		int number = (int) request.getSession().getAttribute("number");
		return "redirect:searchAccessList.action?number=" + number;
	}

	/**
	 * logical Delete Access
	 * 
	 * @param request
	 * @param response
	 * @param accessId
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "logicalDeleteAccess.action")
	public String logicalDeleteAccess(
			@RequestParam("accessId") Integer accessId,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		accessService.logicalDeleteAccess(accessId);
		int number = (int) request.getSession().getAttribute("number");
		return "redirect:searchAccessList.action?number=" + number;
	}

	// when use date, end and front will convert string to date, and convert
	// date to string,
	// follow method will resolve error which above mentioned.


	@InitBinder
	protected void initBinder(WebDataBinder binder) {
		binder.registerCustomEditor(Integer.class, null, new IntegerEditor());
		binder.registerCustomEditor(int.class, null, new IntegerEditor());
		
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		binder.registerCustomEditor(Date.class, new CustomDateEditor(
				dateFormat, true));

	}

}
